package com.express.controller;

import com.express.annotation.AuthorityCheck;
import com.express.dto.ApiClassDto;
import com.express.dto.ApiFormDto;
import com.express.entity.SysUserTokenEntity;
import com.express.service.SysApiService;
import com.express.util.JWTService;
import com.express.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 系统api
 *
 * @author Rock.Zhou
 * @email cool_fish@aliyun.com
 * @date 2020-01-18 20:07:56
 */
@RestController
@RequestMapping("/sysApi")
public class SysApiController {

    /**
     * api
     */
    @Autowired
    private SysApiService apiService;

    /**
     * jwt
     */
    @Autowired
    private JWTService jwtService;


    /**
     * 根据用户获取api菜单
     *
     * @return
     */
    @GetMapping("/getApiList")
    @AuthorityCheck("/sysApi/getApiList")
    @ResponseBody
    public R getApiList(@RequestHeader("Authentication") String token) {
        //获取用户信息
        SysUserTokenEntity user = jwtService.getUser(token);
        if (null == user || null == user.getUserNo()) {
            return R.error("无效token");
        }
        long userId = user.getId();

        // 获取api列表
        List<ApiClassDto> apiList = new ArrayList<>();
        // 根据用户id获取api菜单
        apiList = apiService.getApiListByUserId(userId);

        return R.ok().put("apiList", apiList);
    }

    /**
     * apiItem表单新增
     *
     * @param apiFormDto
     * @return
     */
    @RequestMapping("/saveApiForm")
    @ResponseBody
    public R saveApiForm(@RequestHeader("Authentication") String token, @RequestBody ApiFormDto apiFormDto) {

        // 判断同模块下名称是否存在
        if (null != apiFormDto.getApiName() && !"".equals(apiFormDto.getApiName())){
            String apiName = apiFormDto.getApiName();
            Long apiClassId = apiFormDto.getApiClassId();
            int countByName = apiService.findByApiName(apiName,apiClassId);
            if (countByName > 0){
                return R.error("接口名称已经存在！");
            }
        }


        // 获取用户信息
        SysUserTokenEntity user = jwtService.getUser(token);
        if (null == user || null == user.getUserNo()) {
            return R.error("无效token");
        }
        long userId = user.getId();

        // set添加人
        apiFormDto.setCreateId(userId);
        apiFormDto.setCreateTime(new Date());

        // 新增一个api详情表单
        long apiId = apiService.saveApiForm(apiFormDto);

        if (apiId <= 0) {
            return R.error("新增失败!");
        }

        return R.ok("新增成功!").put("apiId", apiId);
    }

    /**
     * 根据apiId获取apiItem
     *
     * @param token
     * @param apiId
     * @return
     */
    @GetMapping("/getApiItemById")
    @ResponseBody
    public R getApiItemById(@RequestHeader("Authentication") String token, @RequestParam("apiId") Long apiId) {
        //获取用户信息
        SysUserTokenEntity user = jwtService.getUser(token);
        if (null == user || null == user.getUserNo()) {
            return R.error("无效token");
        }
        long userId = user.getId();
        // 校验是否有权限查询api
        int checkCount = apiService.checkApiVerify(apiId, userId);
        if (checkCount < 1) {
            return R.error("该账号无权限查看接口详情!");
        }
        // 查询详情
        ApiFormDto apiFormDto = apiService.getApiItemById(apiId);
        if (null == apiFormDto.getId()){
            return R.error("接口不存在");
        }
        return R.ok().put("apiItem", apiFormDto);
    }

    /**
     * 修改接口信息
     * @param token
     * @param apiFormDto
     * @return
     */
    @PostMapping("/updateApiItem")
    @ResponseBody
    public R updateApiItem(@RequestHeader("Authentication")String token, @RequestBody ApiFormDto apiFormDto){
        if (null == apiFormDto.getId()){
            return R.error("接口Id不存在，修改失败！");
        }
        Long apiId = apiFormDto.getId();

        // 获取用户信息
        SysUserTokenEntity user = jwtService.getUser(token);
        if (null == user || null == user.getUserNo()) {
            return R.error("无效token");
        }
        long userId = user.getId();
        // 校验是否有权限查询api
        int checkCount = apiService.checkApiVerify(apiId, userId);
        if (checkCount < 1) {
            return R.error("该账号无权限修改接口详情!");
        }

        // 修改接口信息
        apiService.updateItemByForm(apiFormDto);

        return R.ok("接口信息修改成功！");
    }

}
